package com.project.movie.controller.performer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.movie.common.enums.ResultCode;
import com.project.movie.domain.MoviePerformer;
import com.project.movie.domain.Result;
import com.project.movie.service.MoviePerformerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 演员controller
 * @date 2025/02/06 10:49
 */
@Controller
@ResponseBody
@RequestMapping("performer")
public class MoviePerformerController {

    @Autowired
    private MoviePerformerService moviePerformerService;

    /** 分页获取演员 */
    @PostMapping("getMoviePerformerPage")
    public Result getMoviePerformerPage(@RequestBody MoviePerformer moviePerformer) {
        Page<MoviePerformer> page = new Page<>(moviePerformer.getPageNumber(),moviePerformer.getPageSize());
        QueryWrapper<MoviePerformer> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotBlank(moviePerformer.getName()),MoviePerformer::getName,moviePerformer.getName())
                .like(StringUtils.isNotBlank(moviePerformer.getRole()),MoviePerformer::getRole,moviePerformer.getRole())
                .eq(StringUtils.isNotBlank(moviePerformer.getItemId()),MoviePerformer::getItemId,moviePerformer.getItemId());
        Page<MoviePerformer> moviePerformerPage = moviePerformerService.page(page, queryWrapper);
        return Result.success(moviePerformerPage);
    }

    @PostMapping("getMoviePerformerList")
    public Result getMoviePerformerList(@RequestBody MoviePerformer moviePerformer) {
        QueryWrapper<MoviePerformer> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotBlank(moviePerformer.getName()),MoviePerformer::getName,moviePerformer.getName())
                .like(StringUtils.isNotBlank(moviePerformer.getRole()),MoviePerformer::getRole,moviePerformer.getRole())
                .eq(StringUtils.isNotBlank(moviePerformer.getItemId()),MoviePerformer::getItemId,moviePerformer.getItemId());
        List<MoviePerformer> moviePerformerPage = moviePerformerService.list(queryWrapper);
        return Result.success(moviePerformerPage);
    }

    /** 根据id获取演员 */
    @GetMapping("getMoviePerformerById")
    public Result getMoviePerformerById(@RequestParam("id")String id) {
        MoviePerformer moviePerformer = moviePerformerService.getById(id);
        return Result.success(moviePerformer);
    }

    /** 保存演员 */
    @PostMapping("saveMoviePerformer")
    public Result saveMoviePerformer(@RequestBody MoviePerformer moviePerformer) {
        boolean save = moviePerformerService.save(moviePerformer);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑演员 */
    @PostMapping("editMoviePerformer")
    public Result editMoviePerformer(@RequestBody MoviePerformer moviePerformer) {
        boolean save = moviePerformerService.updateById(moviePerformer);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除演员 */
    @GetMapping("removeMoviePerformer")
    public Result removeMoviePerformer(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                moviePerformerService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("演员id不能为空！");
        }
    }

}
